package com.gitee.cirnochat.login.controller;

import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.login.model.vo.resp.LoginHistoryVo;
import com.gitee.cirnochat.login.service.LoginHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "登录历史记录相关接口", description = "登录历史记录相关API")
@RestController
@RequestMapping("/history/login")
@RequiredArgsConstructor
public class LoginHistoryController {
    private final LoginHistoryService loginHistoryService;

    @Operation(summary = "获取登录历史记录", description = "获取30天内的登录历史记录")
    @ApiResponse(responseCode = "200", description = "成功获取登录历史记录",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(
                            schema = @Schema(implementation = LoginHistoryVo.class)
                    )
            )
    )
    @GetMapping
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<List<LoginHistoryVo>> getLoginHistory(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(loginHistoryService.getLoginHistory(30, userId));
    }

    @Operation(summary = "删除指定登录历史记录", description = "根据记录ID删除指定的登录历史记录")
    @ApiResponse(responseCode = "200", description = "成功删除指定登录历史记录")
    @DeleteMapping("/delete/{historyId}")
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Void> deleteLoginHistory(@PathVariable Long historyId, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        loginHistoryService.deleteLoginHistory(historyId, userId);
        return Result.success();
    }

    @Operation(summary = "清空登录历史记录", description = "清空当前用户的所有登录历史记录")
    @ApiResponse(responseCode = "200", description = "成功清空登录历史记录")
    @DeleteMapping("/delete/all")
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Void> clearLoginHistory(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        loginHistoryService.clearLoginHistory(userId);
        return Result.success();
    }
}
